// pages/home/classify.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    category_list: [],
    good_list: [],
    uid: 0,
    page: 1,
    page_size: 15,
    current_index: 0,
    default_seacrhing_list: [{
      name:'妇科千金片'
    },{
      name:'私处洗液'
    },{
      name:'加味逍遥丸'
    }],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: util.getAppValue(App.globalData.uid)
    })
    // let index = options.index
    // this.setData({
    //   current_index: index,
    // })
    this.get_category_list()
  },
  go_search() {
    wx.navigateTo({
      url: '/pages/good/search',
    })
  },
  cart_add(){
    util.showLoading("正在添加")
    let params = {
      // goods_num: this.data.cart_num,
      goods_id: this.data.goods_id,
      uid: this.data.uid
    }
    request.post(api.cart_add, params).then(res=>{
      this.setData({
        if_show_box:false
      })
      util.showToast("已加入购物车")
    })
  },
  go_detail_from_component(e) {
    let id = e.detail.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/good/detail?id=' + id,
    })
  },
  go_detail(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/good/detail?id=' + id + "&entrance=hot_good_around",
    })
  },
  get_category_list(index=0) {
    let params = {
      uid: this.data.uid,
      page: 1,
      pageSize: 10,
    }
    request.post(api.category_list, params).then(res=>{
      // res.reverse()
      let category_all = {
        id: 0,
        visibility: 1,
        name: "全部推荐",
      }
      let category_list = []
      res.unshift(category_all)
      this.setData({
        category_list: res
      })
      let type = res[index].id
      this.get_good_list(type)
    })
  },
  get_good_list(type) {
    let params = {
      uid: this.data.uid,
      page: this.data.page,
      pageSize: this.data.page_size,
      type,
    }
    request.post(api.goods_list, params).then(res=>{
      this.setData({
        good_list: res
        // good_list: [
        // {
        //   name: '健胃消食片',
        //   sell_price: 38,
        //   img: "http://img.gzzsu.com/shop/220329/1648537069157.jpg"
        // },{
        //   name: '健胃消食片',
        //   sell_price: 38,
        //   img: "http://img.gzzsu.com/shop/220329/1648537069157.jpg"
        // },{
        //   name: '健胃消食片',
        //   sell_price: 38,
        //   img: "http://img.gzzsu.com/shop/220329/1648537069157.jpg"
        // },{
        //   name: '健胃消食片',
        //   sell_price: 38,
        //   img: "http://img.gzzsu.com/shop/220329/1648537069157.jpg"
        // },{
        //   name: '健胃消食片',
        //   sell_price: 38,
        //   img: "http://img.gzzsu.com/shop/220329/1648537069157.jpg"
        // },{
        //   name: '健胃消食片',
        //   sell_price: 38,
        //   img: "http://img.gzzsu.com/shop/220329/1648537069157.jpg"
        // },{
        //   name: '健胃消食片',
        //   sell_price: 38,
        //   img: "http://img.gzzsu.com/shop/220329/1648537069157.jpg"
        // },]
      })
    })
  },

  choosing_category(e) {
    let current_index = e.currentTarget.dataset.index
    this.setData({
      current_index
    })
    let type = e.currentTarget.dataset.cate_id
    this.get_good_list(type)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})